import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/10 8:26
 * @description:
 */
public class Solution1004 {
    public int longestOnes(int[] nums, int k) {
        int left = 0;
        int res = 0;
        int windowZeroCount = 0;
        for (int right = 0; right < nums.length; right++) {
            if (nums[right] == 0) {
                windowZeroCount++;
            }
            if (windowZeroCount <= k) {
                res = Math.max(res, right - left + 1);
            }
            while (windowZeroCount > k) {
                if(nums[left]==0){
                    windowZeroCount--;
                }
                left++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Solution1004 solution = new Solution1004();
        System.out.println(solution.longestOnes(new int[]{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}, 2));
        System.out.println(solution.longestOnes(new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3));
    }
}
